//
//  CityViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/11.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "CityViewController.h"
#import "UserDefaultUtil.h"
#import "HttpReauestParam.h"
#import "AFHTTPRequestOperationManager.h"
#import "MBProgressHUD+MJ.h"
#import "CityTableViewCell.h"
#import "UIColor+Hex.h"
#import "AuthLocationViewController.h"
@interface CityViewController ()

@property NSArray *data1;
@property NSArray *data2;
@property NSInteger pos;
@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.table_view.delegate = self;
    self.table_view.dataSource = self;
    
   
    
    UINib *nib = [UINib nibWithNibName:@"CityTableViewCell" bundle:nil];
    [self.table_view registerNib:nib forCellReuseIdentifier:@"city_cell"];
    
    
    if ([self.table_view respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.table_view setSeparatorInset:UIEdgeInsetsZero];
    }
    self.table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.table_view.tableHeaderView.frame = CGRectMake(_table_view.tableHeaderView.frame.origin.x, _table_view.tableHeaderView.frame.origin.y, 0, 0);
    
    [self getData:@"" type:@"0"];

}

-(void)viewDidLayoutSubviews
{
    if ([self.table_view respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.table_view setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.table_view respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.table_view setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
  
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CityTableViewCell *cell = [_table_view dequeueReusableCellWithIdentifier:@"city_cell"];
    
    NSDictionary *dic ;
    
    if (_pos == 0) {
        dic = _data1[indexPath.row];
    }else{
        dic = _data2[indexPath.row];
    }
    cell.city_name.text = dic[@"regionName"];
    cell.line.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    
    return cell;
}
//点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_pos == 0) {
        NSDictionary *dic = _data1[indexPath.row];
        [self getData:dic[@"regionName"] type:@"1"];
        _pos = 1;
    }else{
        NSDictionary *dic = _data2[indexPath.row];

        AuthLocationViewController *vc = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
        vc.cityStr = dic[@"regionName"];
        [self.navigationController popToViewController:vc animated:YES];
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_pos == 0) {
        return _data1.count;
    }else{
        return _data2.count;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back_click:(id)sender {
    if (_pos == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        _pos = 0;
        [_table_view reloadData];
    }
    
}

-(void)getData:(NSString *) regionName type:(NSString *)type{
    NSString *userId = [UserDefaultUtil getUserId];
    
    
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:userId forKey:@"identifier"];
    [dic setObject:regionName forKey:@"regionName"];
    [dic setObject:type forKey:@"type"];
    
    
    HttpReauestParam *param = [[HttpReauestParam alloc] initWithData:@"county_level_city_get" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        if ([data[@"success"] boolValue]) {
            
            if (_pos == 0) {
                _data1 = data[@"result"];
            }else{
                _data2 = data[@"result"];
            }
            
            
            
            [_table_view reloadData];
            
        }else{
            [MBProgressHUD showError:data[@"errorMsg"]];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络连接失败"];
    }];
    
}
-(NSString*)dictionaryToJson:(id)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
@end
